package com.ljkj.blockpay.presenter;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.JsonResponseListener;
import com.kongzue.baseokhttp.util.JsonMap;
import com.ljkj.blockpay.basedata.BasePresenterImpl;
import com.ljkj.blockpay.model.CommonModel;
import com.ljkj.blockpay.model.UserModel;
import com.ljkj.blockpay.network.ApiToCConfig;
import com.ljkj.blockpay.network.MainRepository;
import com.ljkj.blockpay.network.response.BaseResponse;
import com.ljkj.blockpay.network.response.DisposableCallBack;
import com.ljkj.blockpay.presenter.inter.RegisterInter;
import com.ljkj.blockpay.util.Preferences;
import com.luck.picture.lib.tools.ToastUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by yp on 2022/3/3
 */
public class RegisterPresenter extends BasePresenterImpl<RegisterInter.RegisterInterView> implements RegisterInter.RegisterPresenter{

    public String phone;
    public String email;


    public RegisterPresenter(@NonNull Context context, @NonNull RegisterInter.RegisterInterView view) {
        super(context, view);
        phone = "";
        email = "";
    }


    @Override
    public void userRegister(String code, String countryCode, String email, String mobile, String password, String userType) {
        MainRepository.getInstance().userRegister(code, countryCode, email, mobile, password, userType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(value->{
                    getView().registersuccess();
                },throwable -> {
                    Log.d("zdl","===throwable=="+throwable.getMessage());
                });
//        addDisposable(MainRepository.getInstance().userRegister(code, countryCode, email, mobile, password, userType)
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribeWith(new DisposableCallBack<UserModel>() {
//                    @Override
//                    protected void onSafeSuccess(UserModel model) {
//                        getView().registersuccess();
////                        SaveModelToSPUtil.saveUserToken(model.getToken());
////                        SaveModelToSPUtil.saveUserInfo(model);
//                    }
//
//                    @Override
//                    protected void onSafeFailed(String code, String message) {
//                        super.onSafeFailed(code, message);
//
//                    }
//                }));

    }

    @Override
    public void getCode(String countryCode, String email, String mobile, String tempCode) {
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("countryCode", countryCode);
        jsonMap.put("email", email);
        jsonMap.put("mobile", mobile);
        jsonMap.put("tempCode", tempCode);
        HttpRequest.JSONPOST(getContext(), ApiToCConfig.URL_SEND_CODE, jsonMap, new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (error == null){
                    getView().getCodeSuccess();
                }
            }
        });
        //获取一下验证码
//        addDisposable(MainRepository.getInstance().getCode(countryCode, email, mobile, tempCode)
//        .observeOn(AndroidSchedulers.mainThread())
//        .subscribeWith(new DisposableCallBack<UserModel>(){
//            @Override
//            public void onSuccess(BaseResponse<UserModel> baseResponse) {
//                super.onSuccess(baseResponse);
//                getView().getCodeSuccess();
//            }
//
//            @Override
//            protected void onFailed(String code, String msg) {
//                super.onFailed(code, msg);
//
//            }
//        }));
    }

    /**
     * 获取国家地区代码
     */
    @Override
    public void getCountrySuccess() {
        addDisposable(MainRepository.getInstance().getValidateCode()
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeWith(new DisposableCallBack<List<CommonModel>>(){

            @Override
            public void onSuccess(BaseResponse<List<CommonModel>> baseResponse) {
                super.onSuccess(baseResponse);
                getView().countrySuccess(baseResponse.data);
            }

            @Override
            protected void onSafeFailed(String code, String message) {
                super.onSafeFailed(code, message);
            }
        }));
    }

}
